package com.dubito.muxin.service.impl;

import com.dubito.muxin.dao.UsersMapper;
import com.dubito.muxin.model.Users;
import com.dubito.muxin.service.UserService;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liliang
 * @description:
 * @date 2018/11/18.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private Sid sid;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean queryUsernameIsExist(String username) {
        Users users = usersMapper.selectByUsername(username);
        return users != null;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Users queryUserForLogin(String username, String password) {
        return usersMapper.selectByUsernameAndPassword(username, password);
    }

    @Override
    public Users saveUser(Users users) {
        users.setId(sid.nextShort());
        usersMapper.insertSelective(users);
        return users;
    }
}
